var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/table/child/alert.tsx
var alert_exports = {};
__export(alert_exports, {
  TableAlert: () => TableAlert
});
module.exports = __toCommonJS(alert_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_common = require("../base/common");
var import_util = require("../util");
var numberStyle = { fontWeight: 600, color: "#000" };
var supportType = { avg: ["平均"], max: ["最大"], min: ["最小"], sum: ["总"], totalSummary: ["总", "sum"] };
var defaultState = { selectedRows: [], selectedRowKeys: [], aggregateColumns: [] };
var TableAlert = import_react.default.memo(({ table }) => {
  const pageCahce = (0, import_react.useRef)({ state: defaultState });
  const { pageIndex, pageSize } = table.state.params;
  const [state, setState] = (0, import_util.useRefState)(defaultState);
  const ds = (0, import_react.useContext)(import_common.DsContext);
  const checkDataIndex = table.getCheckBoxDataIndex();
  (0, import_react.useMemo)(() => {
    pageCahce.current.state = state;
  }, [pageIndex, pageSize]);
  const newState = (0, import_react.useMemo)(() => {
    const innerState = {
      selectedRows: [...pageCahce.current.state.selectedRows],
      selectedRowKeys: [...pageCahce.current.state.selectedRowKeys],
      aggregateColumns: []
    };
    table.getAggregateData().forEach((r) => {
      const keyV = r[table.getKeyField()];
      const startIndex = pageCahce.current.state.selectedRowKeys.indexOf(keyV);
      if (startIndex > -1) {
        r[checkDataIndex] = true;
        pageCahce.current.state.selectedRowKeys.splice(startIndex, 1);
        pageCahce.current.state.selectedRows.splice(startIndex, 1);
      } else if (r[checkDataIndex]) {
        innerState.selectedRows.push(r);
        innerState.selectedRowKeys.push(keyV);
      }
    });
    table.alertObj.state = innerState;
    return innerState;
  }, [ds, checkDataIndex]);
  (0, import_react.useEffect)(() => {
    const { normalColumns, fixedColumns } = table.generateColumns();
    const allColumns = [...normalColumns, ...fixedColumns.left.columns, ...fixedColumns.right.columns];
    allColumns.forEach((c) => {
      if (!c.hidden && c.dataIndex && c.aggregates) {
        const supportAggregates = c.aggregates.filter((a) => supportType.hasOwnProperty(a.type || a));
        if (supportAggregates.length) {
          newState.aggregateColumns.push({
            dataIndex: c.dataIndex,
            aggregates: supportAggregates.map((ag) => ({
              label: getAgName(c, ag, table),
              type: supportType[ag.type || ag][1] || ag.type || ag,
              encrypted: c.encrypted,
              formatter(value, options) {
                return import_util.util.isFunction(ag.formatter) ? ag.formatter(value, options) : value;
              }
            }))
          });
        }
      }
    });
    setState(newState);
  }, [newState]);
  (0, import_react.useEffect)(() => {
    table.alertObj.state = state;
  }, [state]);
  (0, import_util.useUpdateEffect)(() => {
    pageCahce.current.state.selectedRowKeys.length && table.refreshView({});
  }, [pageCahce.current.state]);
  (0, import_react.useEffect)(() => {
    return table.subscribe(() => {
      table.alertObj.state = defaultState;
      pageCahce.current.state = defaultState;
      setState(defaultState);
    }, "clearCacheCheckState");
  }, []);
  const isMessageRender = state.selectedRowKeys.length > (table.settingContainer ? 0 : 1);
  const messageRender = () => {
    if (isMessageRender) {
      const tableAlertRender = import_util.util.isFunction(table.props.tableAlertRender) ? table.props.tableAlertRender : ({ selectedRowKeys, selectedRows, onClear }) => {
        return /* @__PURE__ */ import_react.default.createElement("div", { style: { display: "flex", alignItems: "center", gap: 20, fontSize: 12 } }, /* @__PURE__ */ import_react.default.createElement("div", null, "已选 ", /* @__PURE__ */ import_react.default.createElement("span", { style: numberStyle }, selectedRowKeys.length), " 项", /* @__PURE__ */ import_react.default.createElement("a", { onClick: onClear, style: { marginInlineStart: 8 } }, "取消选择")), state.aggregateColumns.map((c) => /* @__PURE__ */ import_react.default.createElement("div", { key: c.dataIndex }, c.aggregates.map(({ label, type, formatter, encrypted }, index) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: type }, index > 0 && /* @__PURE__ */ import_react.default.createElement(import_antd.Divider, { type: "vertical" }), label, /* @__PURE__ */ import_react.default.createElement("span", { style: numberStyle }, formatter(import_util.util.getAggregate(encrypted ? "encrypted" : type, selectedRows, c.dataIndex), {
          type,
          dataIndex: c.dataIndex,
          data: selectedRows
        })))))));
      };
      return tableAlertRender({
        selectedRowKeys: state.selectedRowKeys,
        selectedRows: state.selectedRows,
        onClear: () => table.clearSelected()
      });
    }
    return null;
  };
  const render = (layout = false) => {
    const content = messageRender();
    const message = layout ? /* @__PURE__ */ import_react.default.createElement(import_util.Layout, { direction: "row" }, /* @__PURE__ */ import_react.default.createElement(import_util.Layout.Flex, null, content), /* @__PURE__ */ import_react.default.createElement(Extra, { container: table.settingContainer })) : content;
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Alert, { message, type: "info", showIcon: !!content, style: { minHeight: 36 } });
  };
  if (table.settingContainer) {
    return render(true);
  }
  if (isMessageRender) {
    return render(false);
  }
  return null;
});
function Extra({ container }) {
  const innerRef = (0, import_react.useRef)(null);
  (0, import_react.useLayoutEffect)(() => {
    if (container && innerRef.current) {
      innerRef.current.appendChild(container);
      return () => {
        var _a;
        return (_a = innerRef.current) == null ? void 0 : _a.removeChild(container);
      };
    }
  }, []);
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: innerRef, style: { display: "flex", minHeight: 18, alignItems: "center", fontSize: 12 } });
}
function getAgName(c, ag, table) {
  const title = import_util.util.isFunction(c.header) ? c.header({ title: c.title, dataIndex: c.dataIndex, column: c, table }) : c.header || c.title;
  return `${supportType[ag.type || ag][0]}${title}: `;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  TableAlert
});
